
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>SpringBoot单元测试 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchors/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-sidebar-style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="设计模式.html" />
    
    
    <link rel="prev" href="SpringMVC.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    前言
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">基础篇</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../java基础/IO流基础.html">
            
                <a href="../java基础/IO流基础.html">
            
                    
                    IO流基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../java基础/集合.html">
            
                <a href="../java基础/集合.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../java基础/异常处理.html">
            
                <a href="../java基础/异常处理.html">
            
                    
                    异常处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../java基础/线程.html">
            
                <a href="../java基础/线程.html">
            
                    
                    线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../java基础/网络编程.html">
            
                <a href="../java基础/网络编程.html">
            
                    
                    网络编程
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">进阶篇</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="MySQL.html">
            
                <a href="MySQL.html">
            
                    
                    MySQL
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="Oracle.html">
            
                <a href="Oracle.html">
            
                    
                    Oracle
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="Redis.html">
            
                <a href="Redis.html">
            
                    
                    Redis
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="Netty.html">
            
                <a href="Netty.html">
            
                    
                    Netty
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="Spring.html">
            
                <a href="Spring.html">
            
                    
                    Spring
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="SpringMVC.html">
            
                <a href="SpringMVC.html">
            
                    
                    SpringMVC
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="3.7" data-path="单元测试.html">
            
                <a href="单元测试.html">
            
                    
                    SpringBoot单元测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="设计模式.html">
            
                <a href="设计模式.html">
            
                    
                    二十三个设计模式
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.8.1" data-path="工厂模式.html">
            
                <a href="工厂模式.html">
            
                    
                    工厂模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.8.2" data-path="单例模式.html">
            
                <a href="单例模式.html">
            
                    
                    单例模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.8.3" data-path="建造者模式.html">
            
                <a href="建造者模式.html">
            
                    
                    建造者模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.8.4" data-path="原型模式.html">
            
                <a href="原型模式.html">
            
                    
                    原型模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.8.5" data-path="适配器模式.html">
            
                <a href="适配器模式.html">
            
                    
                    适配器模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.8.6" data-path="责任链模式.html">
            
                <a href="责任链模式.html">
            
                    
                    责任链模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.8.7" data-path="桥接模式.html">
            
                <a href="桥接模式.html">
            
                    
                    桥接模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">高级篇</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../springcloud/1.cloud架构选型.html">
            
                <a href="../springcloud/1.cloud架构选型.html">
            
                    
                    Spring Cloud
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1.1" data-path="../springcloud/2.Provider-支付模块.html">
            
                <a href="../springcloud/2.Provider-支付模块.html">
            
                    
                    Provider-支付模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.2" data-path="../springcloud/3.Consumer-订单模块.html">
            
                <a href="../springcloud/3.Consumer-订单模块.html">
            
                    
                    Consumer-订单模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.3" data-path="../springcloud/4.重构-Common模块.html">
            
                <a href="../springcloud/4.重构-Common模块.html">
            
                    
                    重构-Common模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.4" data-path="../springcloud/5.注册中心-Enreka.html">
            
                <a href="../springcloud/5.注册中心-Enreka.html">
            
                    
                    注册中心-Enreka
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.5" data-path="../springcloud/6.负载均衡-@LoadBalance.html">
            
                <a href="../springcloud/6.负载均衡-@LoadBalance.html">
            
                    
                    负载均衡-@LoadBalance
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.6" data-path="../springcloud/7.更换注册中心-Zookeeper.html">
            
                <a href="../springcloud/7.更换注册中心-Zookeeper.html">
            
                    
                    更换注册中心-Zookeeper
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.7" data-path="../springcloud/8.更换注册中心-Consul.html">
            
                <a href="../springcloud/8.更换注册中心-Consul.html">
            
                    
                    更换注册中心-Consul
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.8" data-path="../springcloud/9.负载均衡-Ribbon.html">
            
                <a href="../springcloud/9.负载均衡-Ribbon.html">
            
                    
                    负载均衡-Ribbon
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.9" data-path="../springcloud/10.服务调用-OpenFeign.html">
            
                <a href="../springcloud/10.服务调用-OpenFeign.html">
            
                    
                    服务调用-OpenFeign
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.10" data-path="../springcloud/11.熔断器-Hystrix.html">
            
                <a href="../springcloud/11.熔断器-Hystrix.html">
            
                    
                    熔断器-Hystrix
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.11" data-path="../springcloud/12.网关-Gateway.html">
            
                <a href="../springcloud/12.网关-Gateway.html">
            
                    
                    网关-Gateway
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.12" data-path="../springcloud/13.配置中心-Config.html">
            
                <a href="../springcloud/13.配置中心-Config.html">
            
                    
                    配置中心-Config
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.13" data-path="../springcloud/14.消息总线-bus.html">
            
                <a href="../springcloud/14.消息总线-bus.html">
            
                    
                    消息总线-Bus
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.14" data-path="../springcloud/15.消息驱动-stream.html">
            
                <a href="../springcloud/15.消息驱动-stream.html">
            
                    
                    消息驱动-Stream
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.15" data-path="../springcloud/16.链路监控-Sleuth.html">
            
                <a href="../springcloud/16.链路监控-Sleuth.html">
            
                    
                    链路监控-Sleuth
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.16" data-path="../springcloud/17.注册中心&配置中心-nacos.html">
            
                <a href="../springcloud/17.注册中心&配置中心-nacos.html">
            
                    
                    注册中心&配置中心-nacos
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.17" data-path="../springcloud/18.限流-sentinel.html">
            
                <a href="../springcloud/18.限流-sentinel.html">
            
                    
                    限流-sentinel
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../java高级/JUC.html">
            
                <a href="../java高级/JUC.html">
            
                    
                    神秘的JUC包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../java高级/Docker.html">
            
                <a href="../java高级/Docker.html">
            
                    
                    Docker
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../java高级/布隆过滤器.html">
            
                <a href="../java高级/布隆过滤器.html">
            
                    
                    布隆过滤器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../java高级/Redission.html">
            
                <a href="../java高级/Redission.html">
            
                    
                    Redission
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="../java高级/MQ.html">
            
                <a href="../java高级/MQ.html">
            
                    
                    消息队列
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="../java高级/JVM.html">
            
                <a href="../java高级/JVM.html">
            
                    
                    JVM
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.8" data-path="../java书籍/《Effective Java》读书笔记.html">
            
                <a href="../java书籍/《Effective Java》读书笔记.html">
            
                    
                    《Effective Java》读书笔记 
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.8.1" data-path="../java书籍/创建和销毁对象.html">
            
                <a href="../java书籍/创建和销毁对象.html">
            
                    
                    创建和销毁对象
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">番外篇</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../java面试/面试题.html">
            
                <a href="../java面试/面试题.html">
            
                    
                    面试题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../番外篇/小程序.html">
            
                <a href="../番外篇/小程序.html">
            
                    
                    微信小程序
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >SpringBoot单元测试</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h3 id="&#x4E00;&#x3001;junit&#x4F9D;&#x8D56;"><a name="&#x4E00;&#x3001;junit&#x4F9D;&#x8D56;" class="plugin-anchor" href="#&#x4E00;&#x3001;junit&#x4F9D;&#x8D56;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E00;&#x3001;JUnit&#x4F9D;&#x8D56;</h3>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-test<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>test<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>
</code></pre>
<h3 id="&#x4E8C;&#x3001;demo"><a name="&#x4E8C;&#x3001;demo" class="plugin-anchor" href="#&#x4E8C;&#x3001;demo"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E8C;&#x3001;Demo</h3>
<pre><code class="lang-java"><span class="hljs-meta">@RunWith</span>(SpringRunner.class)
<span class="hljs-meta">@SpringBootTest</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SimpleTest</span> </span>{
    <span class="hljs-meta">@Test</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doTest</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">int</span> num = <span class="hljs-keyword">new</span> Integer(<span class="hljs-number">1</span>);
        Assert.assertEquals(num, <span class="hljs-number">1</span>);
    }
}
</code></pre>
<h3 id="&#x4E09;&#x3001;&#x6CE8;&#x89E3;&#x8BF4;&#x660E;"><a name="&#x4E09;&#x3001;&#x6CE8;&#x89E3;&#x8BF4;&#x660E;" class="plugin-anchor" href="#&#x4E09;&#x3001;&#x6CE8;&#x89E3;&#x8BF4;&#x660E;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E09;&#x3001;&#x6CE8;&#x89E3;&#x8BF4;&#x660E;</h3>
<ul>
<li>@RunWith&#xFF1A;&#x6807;&#x8BC6;&#x4E3A;JUnit&#x7684;&#x8FD0;&#x884C;&#x73AF;&#x5883;</li>
<li>@SpringBootTest&#xFF1A;&#x83B7;&#x53D6;&#x542F;&#x52A8;&#x7C7B;&#x3001;&#x52A0;&#x8F7D;&#x914D;&#x7F6E;&#xFF0C;&#x786E;&#x5B9A;&#x88C5;&#x8F7D;Spring Boot</li>
<li>@Test&#xFF1A;&#x58F0;&#x660E;&#x9700;&#x8981;&#x6D4B;&#x8BD5;&#x7684;&#x65B9;&#x6CD5;</li>
<li>@BeforeClass&#xFF1A;&#x9488;&#x5BF9;&#x6240;&#x6709;&#x6D4B;&#x8BD5;&#xFF0C;&#x53EA;&#x6267;&#x884C;&#x4E00;&#x6B21;&#xFF0C;&#x4E14;&#x5FC5;&#x987B;&#x4E3A;static void</li>
<li>@AfterClass&#xFF1A;&#x9488;&#x5BF9;&#x6240;&#x6709;&#x6D4B;&#x8BD5;&#xFF0C;&#x53EA;&#x6267;&#x884C;&#x4E00;&#x6B21;&#xFF0C;&#x4E14;&#x5FC5;&#x987B;&#x4E3A;static void</li>
<li>@Before&#xFF1A;&#x6BCF;&#x4E2A;&#x6D4B;&#x8BD5;&#x65B9;&#x6CD5;&#x524D;&#x90FD;&#x4F1A;&#x6267;&#x884C;&#x7684;&#x65B9;&#x6CD5;</li>
<li>@After&#xFF1A;&#x6BCF;&#x4E2A;&#x6D4B;&#x8BD5;&#x65B9;&#x6CD5;&#x524D;&#x90FD;&#x4F1A;&#x6267;&#x884C;&#x7684;&#x65B9;&#x6CD5;</li>
<li>@Ignore&#xFF1A;&#x5FFD;&#x7565;&#x65B9;&#x6CD5;</li>
</ul>
<h3 id="&#x56DB;&#x3001;&#x8D85;&#x65F6;&#x6D4B;&#x8BD5;"><a name="&#x56DB;&#x3001;&#x8D85;&#x65F6;&#x6D4B;&#x8BD5;" class="plugin-anchor" href="#&#x56DB;&#x3001;&#x8D85;&#x65F6;&#x6D4B;&#x8BD5;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x56DB;&#x3001;&#x8D85;&#x65F6;&#x6D4B;&#x8BD5;</h3>
<p>&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF0C;&#x7ED9;Test&#x8BBE;&#x7F6E;timeout&#x5C5E;&#x6027;&#x5373;&#x53EF;&#xFF0C;&#x65F6;&#x95F4;&#x5355;&#x4F4D;&#x4E3A;&#x6BEB;&#x79D2;&#xFF1A;</p>
<blockquote>
<p>@Test(timeout = 1000)</p>
</blockquote>
<h3 id="&#x4E94;&#x3001;&#x65AD;&#x8A00;&#x6D4B;&#x8BD5;"><a name="&#x4E94;&#x3001;&#x65AD;&#x8A00;&#x6D4B;&#x8BD5;" class="plugin-anchor" href="#&#x4E94;&#x3001;&#x65AD;&#x8A00;&#x6D4B;&#x8BD5;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E94;&#x3001;&#x65AD;&#x8A00;&#x6D4B;&#x8BD5;</h3>
<p>&#x65AD;&#x8A00;&#x6D4B;&#x8BD5;&#x4E5F;&#x5C31;&#x662F;&#x671F;&#x671B;&#x503C;&#x6D4B;&#x8BD5;&#xFF0C;&#x662F;&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x7684;&#x6838;&#x5FC3;&#x4E5F;&#x5C31;&#x662F;&#x51B3;&#x5B9A;&#x6D4B;&#x8BD5;&#x7ED3;&#x679C;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;Assert&#x5BF9;&#x8C61;&#x4E2D;&#x7684;&#x65AD;&#x8A00;&#x65B9;&#x6CD5;&#xFF1A;</p>
<ul>
<li>Assert.assertEquals &#x5BF9;&#x6BD4;&#x4E24;&#x4E2A;&#x503C;&#x76F8;&#x7B49;&#xFF1B;</li>
<li>Assert.assertNotEquals &#x5BF9;&#x6BD4;&#x4E24;&#x4E2A;&#x503C;&#x4E0D;&#x76F8;&#x7B49;&#xFF1B;</li>
<li>Assert.assertSame &#x5BF9;&#x6BD4;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#x76F8;&#x7B49;</li>
<li>Assert.assertArrayEquals &#x5BF9;&#x6BD4;&#x4E24;&#x4E2A;&#x6570;&#x7EC4;&#x76F8;&#x7B49;</li>
<li>Assert.assertTrue &#x9A8C;&#x8BC1;&#x8FD4;&#x56DE;&#x662F;&#x5426;&#x4E3A;&#x771F;</li>
<li>Assert.assertFlase &#x9A8C;&#x8BC1;&#x8FD4;&#x56DE;&#x662F;&#x5426;&#x4E3A;&#x5047;</li>
<li>Assert.assertNull &#x9A8C;&#x8BC1;null</li>
<li>Assert.assertNotNull &#x9A8C;&#x8BC1;&#x975E;null</li>
</ul>
<h3 id="&#x516D;&#x3001;web&#x6A21;&#x62DF;&#x6D4B;&#x8BD5;"><a name="&#x516D;&#x3001;web&#x6A21;&#x62DF;&#x6D4B;&#x8BD5;" class="plugin-anchor" href="#&#x516D;&#x3001;web&#x6A21;&#x62DF;&#x6D4B;&#x8BD5;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x516D;&#x3001;Web&#x6A21;&#x62DF;&#x6D4B;&#x8BD5;</h3>
<p>&#x5728;Spring Boot&#x9879;&#x76EE;&#x91CC;&#x9762;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4F7F;&#x7528;JUnit&#x5BF9;web&#x9879;&#x76EE;&#x8FDB;&#x884C;&#x6D4B;&#x8BD5;&#xFF0C;Spring &#x63D0;&#x4F9B;&#x4E86;&#x201C;TestRestTemplate&#x201D;&#x5BF9;&#x8C61;&#xFF0C;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x53EF;&#x4EE5;&#x5F88;&#x65B9;&#x4FBF;&#x7684;&#x8FDB;&#x884C;&#x6A21;&#x62DF;&#x8BF7;&#x6C42;&#x3002;</p>
<p>Web&#x6D4B;&#x8BD5;&#x53EA;&#x9700;&#x8981;&#x8FDB;&#x884C;&#x4E24;&#x6B65;&#x64CD;&#x4F5C;&#xFF1A;</p>
<ul>
<li><p>&#x5728;<code>@SpringBootTest</code>&#x6CE8;&#x89E3;&#x4E0A;&#x8BBE;&#x7F6E; <code>&#x201C;webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT&#x201D;</code>&#x968F;&#x673A;&#x7AEF;&#x53E3;&#x3002;</p>
</li>
<li><p>&#x4F7F;&#x7528;TestRestTemplate&#x8FDB;&#x884C;post&#x6216;get&#x8BF7;&#x6C42;&#x3002;</p>
</li>
</ul>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-java"><span class="hljs-meta">@RunWith</span>(SpringRunner.class)
<span class="hljs-meta">@SpringBootTest</span>(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserControllerTest</span> </span>{
    <span class="hljs-meta">@Autowired</span>
    <span class="hljs-keyword">private</span> TestRestTemplate restTemplate;
    <span class="hljs-meta">@Test</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">getName</span><span class="hljs-params">()</span> </span>{
        String name = restTemplate.getForObject(<span class="hljs-string">&quot;/name&quot;</span>, String.class);
        System.out.println(name);
        Assert.assertEquals(<span class="hljs-string">&quot;Adam&quot;</span>, name);
    }
}
</code></pre>
<p>&#x5176;&#x4E2D;getForObject&#x7684;&#x542B;&#x4E49;&#x4EE3;&#x8868;&#x6267;&#x884C;get&#x8BF7;&#x6C42;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;Object&#x7ED3;&#x679C;&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x8BBE;&#x7F6E;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#x4E3A;String&#x7C7B;&#x578B;&#xFF0C;&#x66F4;&#x591A;&#x7684;&#x8BF7;&#x6C42;&#x65B9;&#x6CD5;&#xFF1A;</p>
<ul>
<li>getForEntity&#xFF1A;Get&#x8BF7;&#x6C42;&#xFF0C;&#x8FD4;&#x56DE;&#x5B9E;&#x4F53;&#x5BF9;&#x8C61;&#xFF08;&#x53EF;&#x4EE5;&#x662F;&#x96C6;&#x5408;&#xFF09;;</li>
<li>postForEntity&#xFF1A;Post&#x8BF7;&#x6C42;&#xFF0C;&#x8FD4;&#x56DE;&#x5B9E;&#x4F53;&#x5BF9;&#x8C61;&#xFF08;&#x53EF;&#x4EE5;&#x662F;&#x96C6;&#x5408;&#xFF09;;</li>
<li>postForObject&#xFF1A;Post&#x8BF7;&#x6C42;&#xFF0C;&#x8FD4;&#x56DE;&#x5BF9;&#x8C61;&#x3002;</li>
</ul>
<h3 id="&#x4E03;&#x3001;&#x6570;&#x636E;&#x5E93;&#x6D4B;&#x8BD5;"><a name="&#x4E03;&#x3001;&#x6570;&#x636E;&#x5E93;&#x6D4B;&#x8BD5;" class="plugin-anchor" href="#&#x4E03;&#x3001;&#x6570;&#x636E;&#x5E93;&#x6D4B;&#x8BD5;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E03;&#x3001;&#x6570;&#x636E;&#x5E93;&#x6D4B;&#x8BD5;</h3>
<p>&#x5728;&#x6D4B;&#x8BD5;&#x6570;&#x636E;&#x64CD;&#x4F5C;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x6211;&#x4EEC;&#x4E0D;&#x60F3;&#x8BA9;&#x6D4B;&#x8BD5;&#x6C61;&#x67D3;&#x6570;&#x636E;&#x5E93;&#xFF0C;&#x4E5F;&#x662F;&#x53EF;&#x4EE5;&#x5B9E;&#x73B0;&#x7684;&#xFF0C;&#x53EA;&#x9700;&#x8981;&#x6DFB;&#x52A0;&#x7ED9;&#x6D4B;&#x8BD5;&#x7C7B;&#x4E0A;&#x6DFB;&#x52A0;<code>&#x201C;@Transactional&#x201D;</code>&#x5373;&#x53EF;&#xFF0C;&#x8FD9;&#x6837;&#x65E2;&#x53EF;&#x4EE5;&#x6D4B;&#x8BD5;&#x6570;&#x636E;&#x64CD;&#x4F5C;&#x65B9;&#x6CD5;&#xFF0C;&#x53C8;&#x4E0D;&#x4F1A;&#x6C61;&#x67D3;&#x6570;&#x636E;&#x5E93;&#x4E86;&#x3002;</p>
<pre><code class="lang-java"><span class="hljs-meta">@Test</span>
<span class="hljs-meta">@Transactional</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">saveTest</span><span class="hljs-params">()</span> </span>{
    User user = <span class="hljs-keyword">new</span> User();
    user.setName(<span class="hljs-string">&quot;Adam&quot;</span>);
    user.setAge(<span class="hljs-number">19</span>);
    user.setPwd(<span class="hljs-string">&quot;123456&quot;</span>);
    userRepository.save(user);
    System.out.println(<span class="hljs-string">&quot;userId:&quot;</span> + user.getId());
    Assert.assertTrue(user.getId()&gt;<span class="hljs-number">0</span>);
}
</code></pre>
<h3 id="&#x516B;&#x3001;idea&#x5FEB;&#x901F;&#x5F00;&#x542F;&#x6D4B;&#x8BD5;"><a name="&#x516B;&#x3001;idea&#x5FEB;&#x901F;&#x5F00;&#x542F;&#x6D4B;&#x8BD5;" class="plugin-anchor" href="#&#x516B;&#x3001;idea&#x5FEB;&#x901F;&#x5F00;&#x542F;&#x6D4B;&#x8BD5;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x516B;&#x3001;Idea&#x5FEB;&#x901F;&#x5F00;&#x542F;&#x6D4B;&#x8BD5;</h3>
<p>&#x5728; IDEA &#x91CC;&#x9762;&#x53EF;&#x4EE5;&#x5FEB;&#x901F;&#x7684;&#x6DFB;&#x52A0;&#x6D4B;&#x8BD5;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x53EA;&#x9700;&#x8981;&#x5728;&#x8981;&#x6D4B;&#x8BD5;&#x7684;&#x7C7B;&#x91CC;&#x9762;&#x53F3;&#x952E;&#x9009;&#x62E9;<code>&#x201C;GoTo&#x201D;</code>&#x70B9;&#x51FB;<code>&#x201C;Test&#x201D;</code>&#xFF0C;&#x9009;&#x62E9;&#x4F60;&#x9700;&#x8981;&#x6D4B;&#x8BD5;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x70B9;&#x51FB;&#x751F;&#x6210;&#x5373;&#x53EF;&#xFF0C;&#x5982;&#x679C;&#x662F;Windows &#x7528;&#x6237;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x9ED8;&#x8BA4;&#x5FEB;&#x6377;&#x952E; <code>&#x201C;Ctrl + Shift + T&#x201D;</code>&#x3002; </p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; mine 2022-2025 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2021-07-05 20:25:55
</span></footer>
<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="SpringMVC.html" class="navigation navigation-prev " aria-label="Previous page: SpringMVC">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="设计模式.html" class="navigation navigation-next " aria-label="Next page: 二十三个设计模式">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"SpringBoot单元测试","level":"3.7","depth":1,"next":{"title":"二十三个设计模式","level":"3.8","depth":1,"path":"java进阶/设计模式.md","ref":"java进阶/设计模式.md","articles":[{"title":"工厂模式","level":"3.8.1","depth":2,"path":"java进阶/工厂模式.md","ref":"java进阶/工厂模式.md","articles":[]},{"title":"单例模式","level":"3.8.2","depth":2,"path":"java进阶/单例模式.md","ref":"java进阶/单例模式.md","articles":[]},{"title":"建造者模式","level":"3.8.3","depth":2,"path":"java进阶/建造者模式.md","ref":"java进阶/建造者模式.md","articles":[]},{"title":"原型模式","level":"3.8.4","depth":2,"path":"java进阶/原型模式.md","ref":"java进阶/原型模式.md","articles":[]},{"title":"适配器模式","level":"3.8.5","depth":2,"path":"java进阶/适配器模式.md","ref":"java进阶/适配器模式.md","articles":[]},{"title":"责任链模式","level":"3.8.6","depth":2,"path":"java进阶/责任链模式.md","ref":"java进阶/责任链模式.md","articles":[]},{"title":"桥接模式","level":"3.8.7","depth":2,"path":"java进阶/桥接模式.md","ref":"java进阶/桥接模式.md","articles":[]}]},"previous":{"title":"SpringMVC","level":"3.6","depth":1,"path":"java进阶/SpringMVC.md","ref":"java进阶/SpringMVC.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["back-to-top-button","code","splitter","expandable-chapters-small","anchors","theme-comscore","tbfed-pagefooter","sidebar-style","pageview-count","popup"],"pluginsConfig":{"sidebar-style":{"author":"梁兴龙","title":"《Java 笔记本》"},"tbfed-pagefooter":{"copyright":"Copyright &copy mine 2022-2025","modify_label":"文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"splitter":{},"search":{},"popup":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"code":{"copyButtons":false},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"theme-comscore":{},"back-to-top-button":{},"pageview-count":{},"expandable-chapters-small":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"anchors":{}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"java进阶/单元测试.md","mtime":"2021-07-05T12:25:55.731Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-08-22T03:22:22.085Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sidebar-style/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

